using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_CreditList_Panel_Ctrl : MonoBehaviour
{
    public Text text;

    public System.Action triggerAction;
    public System.Action createMemberAction;

    public float scrollSpeed = 160;
    public List<string> memberList = new List<string>();

    public int memberIndex = 0;

    public Text text_Prefab;

    private Text currentMenber_Text;

    public RectTransform scroll_Case;
    public Transform create_Point;

    public Image skip_Progress;
    public Text skip_Text;

    public Image blackMask_Image;

    public bool isCreateMemberList;

    public float intervalTime = 1;
    public float time;

    public bool canSkip = false;
    public float skipTime;
    bool isTriggered;

    public IEnumerator skipTextColorIE;


    public void OnTriggerEnter()
    {
        if (!canSkip)
        {
            return;
        }
        if (!skip_Progress.gameObject.activeSelf)
        {
            skip_Progress.gameObject.SetActive(true);
            skipTime = 0;
        }
        float initAlpha = skip_Text.color.a;
        float time = (1 - initAlpha) * 0.5f;
        IEPool_Manager.instance?.KeepTimeToDo(ref skipTextColorIE, time, null, (_time) => {
            skip_Text.color = new Color(1, 1, 1, Mathf.Lerp(initAlpha, 1, 1 - _time / time));
            return true;
        }, () => {
            skip_Text.color = new Color(1, 1, 1, 1);
        });
    }
    public void OnTriggerExit()
    {
        skip_Progress.gameObject.SetActive(false);

        float initAlpha = skip_Text.color.a;
        float time = initAlpha * 0.5f;
        IEPool_Manager.instance?.KeepTimeToDo(ref skipTextColorIE, time, null, (_time) => {
            skip_Text.color = new Color(1, 1, 1, Mathf.Lerp(initAlpha, 0, 1 - _time / time));
            return true;
        }, () => {
            skip_Text.color = new Color(1, 1, 1, 0);
        });
    }
    private void OnDestroy()
    {
        IEPool_Manager.instance?.StopIE(skipTextColorIE);
    }
    public void Init(params object[] objects)
    {
        canSkip = false;
        this.gameObject.SetActive(true);
        time = intervalTime;
        skip_Text.color = new Color(1, 1, 1, 0);
        isTriggered = false;
        triggerAction = (System.Action)objects[0];
        createMemberAction = (System.Action)objects[1];
        if (objects.Length > 2)
        {
            text.text = (string)objects[2];
        }
        if (objects.Length > 3)
        {
            skip_Text.text = (string)objects[3];
        }
        text.color = new Color(1, 1, 1, 0);
        IEnumerator enumerator = null;
        IEPool_Manager.instance?.KeepTimeToDo(ref enumerator, 1, null, (_time) => {
            text.color = new Color(1, 1, 1, Mathf.Lerp(0, 1, 1 - _time / 1f));
            return true;
        }, () => {
            text.color = new Color(1, 1, 1, 1);
            IEPool_Manager.instance?.KeepTimeToDo(ref enumerator, 2, null, (_time) => {
                text.color = new Color(1, 1, 1, Mathf.Lerp(1, 0, 1 - (_time - 1) / 1f));
                return true;
            }, () => {
                text.color = new Color(1, 1, 1, 0);
                createMemberAction?.Invoke();
                SetMember();
                isCreateMemberList = true;
                canSkip = true;
            });
        });
        skip_Progress.gameObject.SetActive(false);
    }

    public void InitMember(params string[] memberList)
    {
        for (int i = 0; i < memberList.Length; i++)
        {
            this.memberList.Add(memberList[i]);
        }

    }
    private void SetMember()
    {
        currentMenber_Text = Instantiate(text_Prefab, create_Point.transform.position, Quaternion.identity, scroll_Case);
        currentMenber_Text.gameObject.SetActive(true);
        string content = "";
        for (int i = 0; i < memberList.Count; i++)
        {
            content += memberList[i];
            content += "\r\n\r\n\r\n";
        }
        currentMenber_Text.text = content;
        currentMenber_Text.rectTransform.sizeDelta = new Vector2(currentMenber_Text.rectTransform.sizeDelta.x, currentMenber_Text.preferredHeight);
    }
    private void Update()
    {
        scroll_Case.anchoredPosition3D -= Vector3.down * Time.deltaTime * scrollSpeed;

        if (memberIndex < memberList.Count && isCreateMemberList)
        {
            time -= Time.deltaTime;
            if (time <= 0)
            {
                time = intervalTime;
                //CreateMember(memberList[memberIndex]);

                memberIndex++;
            }
        }

        if (skip_Progress&&skip_Progress.gameObject.activeInHierarchy)
        {
            skipTime += Time.deltaTime;
            skip_Progress.fillAmount = skipTime;
            if (skipTime >= 1 && !isTriggered)
            {
                isTriggered = true;
                triggerAction?.Invoke();
            }
        }
    }
    public void SwtichMaskPerformance(bool isSwitch, float time = 0, System.Action finishAction = null)
    {
        if (isSwitch)
        {
            blackMask_Image.raycastTarget = true;
        }
        float initAlpha = blackMask_Image.color.a;
        float targetAlpha = isSwitch ? 1 : 0;
        IEPool_Manager.instance.KeepTimeToDo("", time, null, (_time) =>
        {
            blackMask_Image.color = new Color(blackMask_Image.color.r, blackMask_Image.color.g, blackMask_Image.color.b, Mathf.Lerp(initAlpha, targetAlpha, 1 - _time / time));
            return true;
        }, () =>
        {
            blackMask_Image.color = new Color(blackMask_Image.color.r, blackMask_Image.color.g, blackMask_Image.color.b, targetAlpha);
            finishAction?.Invoke();
            if (!isSwitch)
            {
                blackMask_Image.raycastTarget = false;
            }
        });
    }
    public void HideCurrentMenber_TextPerformance(bool isSwitch, float time = 0)
    {
        float initAlpha = currentMenber_Text.color.a;
        float targetAlpha = isSwitch ? 1 : 0;
        IEPool_Manager.instance.KeepTimeToDo("", time, null, (_time) =>
        {
            currentMenber_Text.color = new Color(currentMenber_Text.color.r, currentMenber_Text.color.g, currentMenber_Text.color.b, Mathf.Lerp(initAlpha, targetAlpha, 1 - _time / time));
            return true;
        }, () =>
        {
            currentMenber_Text.color = new Color(currentMenber_Text.color.r, currentMenber_Text.color.g, currentMenber_Text.color.b, targetAlpha);
            if (!isSwitch)
            {
                currentMenber_Text.raycastTarget = false;
            }
        });
    }
    private void OnDisable()
    {
        Destroy(currentMenber_Text);
    }
}
